package View;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Window.Type;
import java.awt.Toolkit;

public class AddClientScreen {

	public JFrame frame;
	private JTextField textFieldUser;
	private JTextField textFieldPassword;
	private static String file = "ClientList.xml";

	/**
	 * Create the application.
	 */
	public AddClientScreen() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage("res\\add-icon.png"));
		frame.setType(Type.POPUP);
		frame.setBounds(100, 100, 387, 222);
		frame.setVisible(true);
		SpringLayout springLayout = new SpringLayout();
		frame.getContentPane().setLayout(springLayout);
		
		JLabel lblUserName = new JLabel("User Name");
		springLayout.putConstraint(SpringLayout.NORTH, lblUserName, 41, SpringLayout.NORTH, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, lblUserName, 34, SpringLayout.WEST, frame.getContentPane());
		lblUserName.setFont(new Font("Tahoma", Font.PLAIN, 12));
		frame.getContentPane().add(lblUserName);
		
		textFieldUser = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, textFieldUser, -3, SpringLayout.NORTH, lblUserName);
		springLayout.putConstraint(SpringLayout.WEST, textFieldUser, 22, SpringLayout.EAST, lblUserName);
		springLayout.putConstraint(SpringLayout.EAST, textFieldUser, -60, SpringLayout.EAST, frame.getContentPane());
		textFieldUser.setFont(new Font("Tahoma", Font.PLAIN, 12));
		frame.getContentPane().add(textFieldUser);
		textFieldUser.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password");
		springLayout.putConstraint(SpringLayout.NORTH, lblPassword, 21, SpringLayout.SOUTH, lblUserName);
		springLayout.putConstraint(SpringLayout.WEST, lblPassword, 0, SpringLayout.WEST, lblUserName);
		lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
		frame.getContentPane().add(lblPassword);
		
		textFieldPassword = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, textFieldPassword, -3, SpringLayout.NORTH, lblPassword);
		springLayout.putConstraint(SpringLayout.WEST, textFieldPassword, 0, SpringLayout.WEST, textFieldUser);
		springLayout.putConstraint(SpringLayout.EAST, textFieldPassword, 0, SpringLayout.EAST, textFieldUser);
		textFieldPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
		textFieldPassword.setColumns(10);
		frame.getContentPane().add(textFieldPassword);
		
		JButton btnOk = new JButton("OK");
		springLayout.putConstraint(SpringLayout.WEST, btnOk, 95, SpringLayout.WEST, frame.getContentPane());
		btnOk.setFont(new Font("Tahoma", Font.PLAIN, 12));
		btnOk.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(textFieldUser.getText() != "" && textFieldPassword.getText() != ""){
					String user = textFieldUser.getText();
					String password = textFieldPassword.getText();
					Client client = new Client(user, password);
					ClientManager manager = new ClientManager();
					
					if(manager.AddClient(file, client)){
						JOptionPane.showMessageDialog(frame, "Add client! Success!!!");
						frame.setVisible(false);
						ClientListScreen.loadListOn = 1;
						ClientListScreen.ViewAll();
					}else{
						JOptionPane.showMessageDialog(frame, "Add client! Fail!");
						
					}
				}else {
					JOptionPane.showMessageDialog(frame, "Please complete client's information!");
				}
			}
			
		});
		frame.getContentPane().add(btnOk);
		
		JButton btnCancel = new JButton("Cancel");
		springLayout.putConstraint(SpringLayout.WEST, btnCancel, 184, SpringLayout.WEST, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, btnCancel, -104, SpringLayout.EAST, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, btnOk, 0, SpringLayout.NORTH, btnCancel);
		springLayout.putConstraint(SpringLayout.EAST, btnOk, -6, SpringLayout.WEST, btnCancel);
		springLayout.putConstraint(SpringLayout.NORTH, btnCancel, 34, SpringLayout.SOUTH, textFieldPassword);
		btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 12));
		btnCancel.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				frame.setVisible(false);
			}
			
		});
		frame.getContentPane().add(btnCancel);
	}
}
